//
//  YZEDoctorDetailViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/23.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import <QMUITips.h>
#import "CommonDefine.h"
#import "YZEDoctorDetailViewController.h"
#import "YZEDoctorDetailViewHeadCell.h"
#import "YZEDoctorDetailViewHeadCell2.h"
#import "YZEDoctorDetailViewDescCell.h"
#import "UIColor+PYSearchExtension.h"

#define URL_Followdoctor @"/api/follows" //关注
#define URL_SendLike @"/api/hlikes" //点赞

@interface YZEDoctorDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIButton *followBtn;
@property(nonnull,retain) UIButton *likeBtnRef;//缓存关注按钮对象，用于设置关注状态
@end

@implementation YZEDoctorDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.navigationController.navigationBarHidden = YES;
    [self.navigationController.navigationBar setTintColor:[UIColor clearColor]];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.allowsSelection = NO;

    if (iOS11Later) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;//与状态栏齐平
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YZEDoctorDetailViewHeadCell" bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"headview"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YZEDoctorDetailViewHeadCell2" bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"headview2"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YZEDoctorDetailViewDescCell" bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"desc"];
    
    [self.followBtn addTarget:self action:@selector(sendFollowReq) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置状态栏白色
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section==0) {
        count = 2;
    }
    else
        count = 5;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0 ){
        if (indexPath.row==0) {
            YZEDoctorDetailViewHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headview"];
            if (!cell) {
                cell = [[YZEDoctorDetailViewHeadCell alloc]init];
            }
            [cell.avatarView sd_setImageWithURL:[NSURL URLWithString:_data.userResponseDto.avatar]];
            cell.doctorNameLabel.text = _data.userResponseDto.firstName;
            cell.jobtitleLabel.text = _data.professionalTitle;
            cell.hospitalnameLabel.text = _data.hospitalDTO.name;
            [cell.likeBtn setTitle:[NSString stringWithFormat:@"%ld", _data.likedCount] forState:UIControlStateNormal];
            self.likeBtnRef = cell.likeBtn;
            [cell.backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
            [cell.likeBtn addTarget:self action:@selector(sendLikereq) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
        else {
            YZEDoctorDetailViewHeadCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"headview2"];
            if (!cell) {
                cell = [[YZEDoctorDetailViewHeadCell2 alloc]init];
            }
            long onlinetime = _data.onlineTime?_data.onlineTime:0;
            onlinetime = onlinetime/60;
            cell.onlinetimeLabel.text = [NSString stringWithFormat:@"%ld小时", onlinetime];
            long answercount = _data.answerCount?_data.answerCount:0;
            cell.answercountLabel.text = [NSString stringWithFormat:@"%ld条", answercount];
            
            return cell;
        }
    }
    else if (indexPath.section==1) {
        switch (indexPath.row) {
            case 0:
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
                cell.textLabel.text = @"图文咨询";
                cell.detailTextLabel.text = _data.canPicAsk?@"服务中":@"暂停服务";
                return cell;
            }
                break;
            case 1:
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
                cell.textLabel.text = @"电话咨询";
                cell.detailTextLabel.text = _data.canTelAsk?@"服务中":@"暂停服务";
                return cell;
            }
                break;
            case 2:
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"];
                cell.textLabel.text = @"门诊时间";
                cell.detailTextLabel.text = _data.workTime;
                return cell;
            }
                break;
            case 3:
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"];
                cell.textLabel.text = @"擅长领域";
                NSMutableArray *valarr = [NSMutableArray array];
                for (int i=0; i<_data.graftings.count; i++) {
                    RPGrafting *grafting = _data.graftings[i];
                    [valarr addObject:grafting.name];
                }
                NSString *valstr = [valarr componentsJoinedByString:@"、"];
                cell.detailTextLabel.text = valstr;
                return cell;
            }
            case 4:
            {
                YZEDoctorDetailViewDescCell *cell = [tableView dequeueReusableCellWithIdentifier:@"desc"];
                if (!cell) {
                    cell = [[YZEDoctorDetailViewDescCell alloc]init];
                }
                
                cell.descTextview.text = _data.Description?_data.Description:@"暂无个人介绍";
                return cell;
            }
            default:
            {
                UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                return cell;
            }
                break;
        }
    }
    else {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat h = 50;
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            h = 210;
        }
        if (indexPath.row==1) {
            h = 66;
        }
    }
    else if (indexPath.section==1){
        if (indexPath.row==4) {
            h = 151;
        }
    }
    
    return h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==1) {
        return 0.01;
    }
    return 15;
}

#pragma mark - 业务逻辑
-(void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//点赞
- (void)sendLikereq{
    NSDictionary *paramDic = @{@"cancel":[NSNumber numberWithBool:!_data.hasLiked], @"dliked":@{@"id":[NSNumber numberWithLong:[_data.userResponseDto.ID longLongValue]]}};
    [[YZENetwork sharedInstace]postRequestWithUrl:[BASE stringByAppendingString:URL_SendLike]
                                       parameters:paramDic
                                         delegate:self
                                       requestTag:1
                                     successBlock:^(id responseObject)
     {
         _data.hasLiked = !_data.hasLiked;
         _data.likedCount = _data.hasLiked?(_data.likedCount + 1):(_data.likedCount - 1);
         NSString *newLikeCount = [NSString stringWithFormat:@"%ld", _data.likedCount];
         [self.likeBtnRef setTitle:newLikeCount forState:UIControlStateNormal];
         NSString *actionStr = _data.hasLiked?@"已点赞":@"已取消点赞";
         [QMUITips showSucceed:[NSString stringWithFormat:@"%@%@医生",actionStr, _data.userResponseDto.firstName] inView:self.view hideAfterDelay:1];
     }];
}

//关注
-(void)sendFollowReq{
    NSDictionary *paramDic = @{@"cancel":[NSNumber numberWithBool:_data.hasFollowed],
                               @"concernedId":[NSNumber numberWithInteger:[_data.userResponseDto.ID integerValue]]};
    [[YZENetwork sharedInstace]postRequestWithUrl:[BASE stringByAppendingString:URL_Followdoctor]
                                       parameters:paramDic
                                         delegate:self
                                       requestTag:1
                                     successBlock:^(id responseObject)
    {
        _data.hasFollowed = !_data.hasFollowed;
        NSString *statusStr = _data.hasFollowed?@"取消关注":@"关注";
        NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:statusStr
                                      attributes:[NSDictionary
                                                  dictionaryWithObjectsAndKeys:[UIColor
                                                                                py_colorWithHexString:@"#2280E9"], NSForegroundColorAttributeName, nil]];
        [self.followBtn setAttributedTitle:attrStr forState:UIControlStateNormal];
        NSString *actionStr = _data.hasFollowed?@"已关注":@"已取消关注";
        [QMUITips showSucceed:[NSString stringWithFormat:@"%@%@医生",actionStr, _data.userResponseDto.firstName] inView:self.view hideAfterDelay:1];
    }];
}

@end
